//
//  AlarmPageViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/27.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import SwiftFontName

class SleepPageViewController: SleepBaseViewController {
    @IBOutlet weak var sleepButton: UIButton! {
        didSet {
            sleepButton.addTarget(self, action: #selector(sleep), for: .touchUpInside)
        }
    }
    @IBOutlet weak var recordButton: UIButton! {
        didSet {
            recordButton.addTarget(self, action: #selector(sleepRecord), for: .touchUpInside)
        }
    }
    @IBOutlet weak var aidSleepButton: UIButton! {
        didSet {
            aidSleepButton.addTarget(self, action: #selector(sleepAid), for: .touchUpInside)
        }
    }
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var alarmButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        addMiddleButton()
    }

    @objc private func sleep() {
        guard let coord = MainCoordinator.shared.childCoordinators.first(where: { $0 is SleepCoordinator }) as? SleepCoordinator
            else { debugPrint("find sleep coordinator error"); return }
        coord.sleep()
    }
    @objc private func sleepRecord() {
        guard let coord = MainCoordinator.shared.childCoordinators.first(where: { $0 is SleepCoordinator }) as? SleepCoordinator
            else { debugPrint("find sleep coordinator error"); return }
        coord.record()
    }
    @objc private func sleepAid() {
        guard let coord = MainCoordinator.shared.childCoordinators.first(where: { $0 is SleepCoordinator }) as? SleepCoordinator
            else { debugPrint("find sleep coordinator error"); return }
        coord.aidSleep()
    }

    private func addMiddleButton() {
        let button = AlarmButton()
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            button.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4),
            button.heightAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1)
            ])

        button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(sleep)))
    }
}

class AlarmButton: UIView {
    var borderLayer: CAShapeLayer!
    var gradientLayer: CAGradientLayer!

    override func layoutSubviews() {
        super.layoutSubviews()
        addBorder()
        addGradientLayer()
        addLabel()
    }

    private func addBorder() {
        guard borderLayer == nil else { return }
        borderLayer = CAShapeLayer()
        borderLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.width / 2).cgPath
        borderLayer.strokeColor = UIColor("#1BBAD1").cgColor
        borderLayer.fillColor = UIColor.clear.cgColor
        layer.addSublayer(borderLayer)
    }

    private func addGradientLayer() {
        guard gradientLayer == nil else { return }
        let circleLayer = CAShapeLayer()
        let circlePath = UIBezierPath(roundedRect: bounds.insetBy(dx: 5, dy: 5),
                                      cornerRadius: bounds.width / 2 - 5)
        circleLayer.path = circlePath.cgPath
        circleLayer.fillColor = UIColor.white.cgColor

        let gradient = CAGradientLayer()
        gradient.frame = bounds
        gradient.colors = [UIColor("#2FBDD2").cgColor,
                           UIColor("#1BBAD1").cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 1)
        gradient.endPoint = CGPoint(x: 1, y: 0)
        gradient.mask = circleLayer
        self.gradientLayer = gradient

        layer.addSublayer(gradient)
    }

    private func addLabel() {
        let label = UILabel()
        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "进入睡眠"
        label.textColor = .white
        label.font = UIFont(name: FontName.PingFangSCRegular, size: 20)
        NSLayoutConstraint.activate([
            label.centerYAnchor.constraint(equalTo: centerYAnchor),
            label.centerXAnchor.constraint(equalTo: centerXAnchor)
            ])
    }
}
